package com.intelligentcar.springboot.controller;

import com.intelligentcar.springboot.bean.City;
import com.intelligentcar.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CityController {

    @Autowired
    CityService cityService;
    //    薪资数据展示
    @RequestMapping("/LookCity")
    @ResponseBody   //加这个注释,直接给数据,跳过视图层
    public Map list(@RequestBody City city) {
        Map map = new HashMap();
        if (city.getInput().equals("") || city.getInput()==""){
            List<City> list = cityService.Lookcity2();
            map.put("list", list);
            map.put("total", cityService.selectcity());
        }
        for(int i=city.getInput().length();--i>=0;) {
            int chr = city.getInput().charAt(i);
            if (chr < 48 || chr > 57) {
                List<City> list = cityService.Lookcity(city.getInput());
                map.put("list", list);
                map.put("total", cityService.selectcity());
            } else {
                city.setAddTime(city.getInput());
                List<City> list = cityService.selectTimecity(city.getAddTime());
                map.put("list", list);
                map.put("total", cityService.selectcity());
            }
        }
        return map;
    }
    //    薪资数据展示
    @RequestMapping("/Lookcity2")
    @ResponseBody   //加这个注释,直接给数据,跳过视图层
    public Map list() {
        List<City> list = cityService.Lookcity2();
        Map map = new HashMap();
        map.put("list", list);
        map.put("total",cityService.selectcity());
        return map;
    }

    // 薪资修改
    @RequestMapping("/updataCity")
    @ResponseBody //跳过视图解析器,直接返回数据
    public int update(@RequestBody City city) {
        int a = cityService.updatecity(city);
        return a;
    }

    //薪资界面中的删除信息操作
    @RequestMapping("/delCity")//请求路径
    @ResponseBody //跳过视图解析器,直接返回数据
    public int delete(@RequestBody Object cityId){
        System.out.println(cityId);
        int delete=cityService.deletecity(cityId);
        if (delete>=1){
            return 1;
        }
        return 0;
    }

    //薪资中的新增信息操作
    @RequestMapping("/addCity")
    @ResponseBody //跳过视图解析器,直接返回数据
    public Map<String, Object> add(@RequestBody City city) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", cityService.addcity(city));
        return map;
    }
}
